<template>
  <view class="page">
    <view class="back-btn" @click="handleBack">
      <uni-icons type="back" size="24" color="#333" />
    </view>

    <view class="header">
      <text class="title">快速问诊</text>
    </view>

    <scroll-view class="content" scroll-y>
      <view class="symptom-section">
        <text class="section-title">选择症状</text>
        <view class="symptom-list">
          <view v-for="(item, index) in symptoms" :key="index" 
                class="symptom-item" 
                :class="{ active: selectedSymptoms.includes(item.id) }"
                @tap="selectSymptom(item.id)">
            <uni-icons :type="item.icon" size="24" color="#2B9FFF"/>
            <text class="symptom-text">{{ item.name }}</text>
          </view>
        </view>
      </view>

      <view class="form-section">
        <text class="section-title">基本信息</text>
        <view class="form-item">
          <text class="label">姓名</text>
          <input type="text" v-model="form.name" placeholder="请输入姓名"/>
        </view>
        <view class="form-item">
          <text class="label">年龄</text>
          <input type="number" v-model="form.age" placeholder="请输入年龄"/>
        </view>
        <view class="form-item">
          <text class="label">性别</text>
          <view class="gender-group">
            <view class="gender-item" :class="{ active: form.gender === '男' }" @tap="selectGender('男')">
              <text>男</text>
            </view>
            <view class="gender-item" :class="{ active: form.gender === '女' }" @tap="selectGender('女')">
              <text>女</text>
            </view>
          </view>
        </view>
      </view>

      <view class="description-section">
        <text class="section-title">病情描述</text>
        <textarea v-model="form.description" 
                  placeholder="请详细描述您的症状，持续时间等..." 
                  :maxlength="200"/>
        <text class="word-count">{{ form.description.length }}/200</text>
      </view>
    </scroll-view>

    <view class="footer">
      <button class="submit-btn" @tap="submitConsultation">立即问诊</button>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue';

const symptoms = [
  { id: 1, name: '发烧', icon: 'info' },
  { id: 2, name: '咳嗽', icon: 'info' },
  { id: 3, name: '头痛', icon: 'info' },
  { id: 4, name: '腹痛', icon: 'info' },
  { id: 5, name: '感冒', icon: 'info' },
  { id: 6, name: '失眠', icon: 'info' },
];

const selectedSymptoms = ref<number[]>([]);
const form = reactive({
  name: '',
  age: '',
  gender: '',
  description: ''
});

const selectSymptom = (id: number) => {
  const index = selectedSymptoms.value.indexOf(id);
  if (index === -1) {
    selectedSymptoms.value.push(id);
  } else {
    selectedSymptoms.value.splice(index, 1);
  }
};

const selectGender = (gender: string) => {
  form.gender = gender;
};

const submitConsultation = () => {
  if (!form.name) {
    uni.showToast({
      title: '请输入姓名',
      icon: 'none'
    });
    return;
  }
  if (!form.age) {
    uni.showToast({
      title: '请输入年龄',
      icon: 'none'
    });
    return;
  }
  if (!form.gender) {
    uni.showToast({
      title: '请选择性别',
      icon: 'none'
    });
    return;
  }
  if (selectedSymptoms.value.length === 0) {
    uni.showToast({
      title: '请选择症状',
      icon: 'none'
    });
    return;
  }
  if (!form.description) {
    uni.showToast({
      title: '请描述病情',
      icon: 'none'
    });
    return;
  }

  uni.showLoading({
    title: '提交中...'
  });
  
  // 模拟提交
  setTimeout(() => {
    uni.hideLoading();
    uni.showToast({
      title: '提交成功',
      icon: 'success'
    });
  }, 1500);
};

const handleBack = () => {
  uni.navigateBack({
    delta: 1
  })
}
</script>

<style>
.back-btn {
  position: fixed;
  top: 60rpx;
  left: 30rpx;
  z-index: 100;
  width: 80rpx;
  height: 80rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 50%;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
}

.back-btn:active {
  transform: scale(0.95);
}

page {
  height: 100%;
}

.page {
  display: flex;
  flex-direction: column;
  height: 100%;
  background-color: #F5F6FA;
}

.header {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 88rpx;
  margin-top: 120rpx;
  background-color: #FFFFFF;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.title {
  font-size: 16px;
  font-weight: 500;
  color: #333333;
}

.content {
  flex: 1;
  overflow: auto;
  padding: 30rpx;
}

.section-title {
  font-size: 16px;
  font-weight: 500;
  color: #333333;
  margin-bottom: 30rpx;
}

.symptom-list {
  display: flex;
  flex-wrap: wrap;
  gap: 20rpx;
  margin-bottom: 40rpx;
}

.symptom-item {
  display: flex;
  align-items: center;
  padding: 20rpx 30rpx;
  background-color: #FFFFFF;
  border-radius: 8px;
  border: 1px solid #E5E6EB;
}

.symptom-item.active {
  background-color: #ECF5FF;
  border-color: #2B9FFF;
}

.symptom-text {
  margin-left: 10rpx;
  font-size: 14px;
  color: #333333;
}

.form-section {
  background-color: #FFFFFF;
  border-radius: 8px;
  padding: 30rpx;
  margin-bottom: 30rpx;
}

.form-item {
  margin-bottom: 30rpx;
}

.form-item:last-child {
  margin-bottom: 0;
}

.label {
  display: block;
  font-size: 14px;
  color: #666666;
  margin-bottom: 20rpx;
}

input {
  width: 100%;
  height: 80rpx;
  padding: 0 20rpx;
  border-radius: 4px;
  border: 1px solid #E5E6EB;
  font-size: 14px;
}

.gender-group {
  display: flex;
  gap: 20rpx;
}

.gender-item {
  flex: 1;
  height: 80rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #E5E6EB;
  border-radius: 4px;
  font-size: 14px;
  color: #333333;
}

.gender-item.active {
  background-color: #ECF5FF;
  border-color: #2B9FFF;
  color: #2B9FFF;
}

.description-section {
  background-color: #FFFFFF;
  border-radius: 8px;
  padding: 30rpx;
  margin-bottom: 120rpx;
}

textarea {
  width: 100%;
  height: 200rpx;
  padding: 20rpx;
  border-radius: 4px;
  border: 1px solid #E5E6EB;
  font-size: 14px;
}

.word-count {
  display: block;
  text-align: right;
  font-size: 12px;
  color: #999999;
  margin-top: 10rpx;
}

.footer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 20rpx 30rpx;
  background-color: #FFFFFF;
  box-shadow: 0 -2px 4px rgba(0, 0, 0, 0.05);
}

.submit-btn {
  width: 100%;
  height: 88rpx;
  line-height: 88rpx;
  background-color: #2B9FFF;
  color: #FFFFFF;
  font-size: 16px;
  font-weight: 500;
  border-radius: 4px;
}

.submit-btn:active {
  opacity: 0.8;
}
</style>

